package com.atmilan.quartz.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.atmilan.quartz.constant.enums.BarCodeEnum;
import com.atmilan.quartz.constant.enums.ProductTypeEnum;
import com.atmilan.quartz.model.dto.crmeb.StoreProductCrmebRequest;
import com.atmilan.quartz.model.dto.jky.JkyProductResDTO;
import com.atmilan.quartz.model.dto.jky.JkyProductResDTO.Goods;
import com.atmilan.quartz.model.dto.jky.JkyProductSkuPriceResDTO;
import com.atmilan.quartz.model.dto.jky.JkyProductSkuPriceResDTO.Skuprice;
import com.atmilan.quartz.model.dto.jky.JkyProductSkuResDTO;
import com.atmilan.quartz.model.dto.jky.JkyProductSkuResDTO.GoodsStockQuantity;
import com.atmilan.quartz.model.entity.buyer.SupplierProductLog;
import com.atmilan.quartz.model.entity.crmeb.Category;
import com.atmilan.quartz.model.entity.crmeb.CategoryMatch;
import com.atmilan.quartz.model.entity.crmeb.StoreProduct;
import com.atmilan.quartz.model.entity.crmeb.StoreProductAttrValue;
import com.atmilan.quartz.model.entity.jky.JkyProductApi;
import com.atmilan.quartz.model.entity.jky.JkyProductSkuApi;
import com.atmilan.quartz.model.request.JackyunGoodsAddRequest;
import com.atmilan.quartz.model.request.JackyunGoodsStockInRequest.StockInDetailView;
import com.atmilan.quartz.model.request.JackyunGoodsStockOutRequest.StockOutDetailView;
import com.atmilan.quartz.service.*;
import com.atmilan.quartz.utils.LogsUtil;
import com.atmilan.quartz.utils.RedisUtil;
import com.atmilan.quartz.utils.jackyun.BaseRequestBizData;
import com.atmilan.quartz.utils.product.JkyProductUtil;
import com.atmilan.quartz.utils.source.SourceJkyProductUtil;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author Howe Hsiang
 */
@Slf4j
@Service
public class JkySyncApiServiceImpl implements IJkySyncApiService {

    @Autowired
    RedisUtil redisUtil;
    @Autowired
    ISystemConfigService systemConfigService;
    @Autowired
    IJkyProductApiService jkyProductApiService;
    @Autowired
    IJkyProductSkuApiService jkyProductSkuApiService;
    @Autowired
    IStoreProductService storeProductService;
    @Autowired
    IStoreProductAttrService storeProductAttrService;
    @Autowired
    IStoreProductAttrValueService storeProductAttrValueService;
    @Autowired
    SourceJkyProductUtil sourceJkyProductUtil;
    @Autowired
    IChannelMerchantService channelMerchantService;
    @Autowired
    ICategoryService categoryService;
    @Autowired
    private IMarkupRateService markupRateService;
    @Autowired
    ICategoryMatchService categoryMatchService;
    @Autowired
    IJkyContrastService jkyContrastService;
    @Autowired
    ISupplierProductLogService supplierProductLogService;
    @Autowired
    JkyProductUtil jkyProductUtil;
    @Autowired
    IZoneProductService zoneProductService;
    // 仓库: 麦斯现货仓, 诚品现货仓, 国现同行真实仓, 086 国现同行同步仓, 欧洲仓, 018意大利仓, tfx淘分销仓
	private static List<String> warehouseCodes = Arrays.asList("001", "066", "075", "025");

    @Autowired
    private LogsUtil logsUtil;

    /**
     * 获取远程数据并保存到数据源
     */
    @Override
    public Boolean syncProduct(String goodsNo,String startDateModifiedSku,String endDateModifiedSku) {
        // 数据清空
        jkyProductApiService.deleteAll();
        jkyProductSkuApiService.deleteAll();
        // 统计
    	Map<String, String> existGoodsNos = new HashMap<>();
    	Map<String, String> existSkuBarcodes = new HashMap<>();
        // 由于吉客云限制, 只能查两次, 首次商品, 二次sku, 重复工作
        selectProduct(false, existGoodsNos, existSkuBarcodes,goodsNo,startDateModifiedSku,endDateModifiedSku);
        selectProduct(true, existGoodsNos, existSkuBarcodes,goodsNo,startDateModifiedSku,endDateModifiedSku);
        
        return true;
    }
    
    /**
     * 查询增量商品
     * @author daigb
     * @date:  2022-09-20 13:35
     */
    private void selectProduct(Boolean isSku, Map<String, String> existGoodsNos, Map<String, String> existSkuBarcodes,
                               String goodsNo,String startDateModifiedSku,String endDateModifiedSku) {

        LocalDate startTime = LocalDate.parse(startDateModifiedSku);            //开始时间
        LocalDate endTime = LocalDate.parse(endDateModifiedSku).plusDays(1);   //结束时间加1

        long millis = System.currentTimeMillis();
//		while (LocalDate.now().plusDays(2).isAfter(endTime)) {
	        int pageNo = 0;
	        int pageSize = 200;
	        int pageCount = 1;
    		
	        while(pageNo < pageCount) {
	            try {
	            	JkyProductResDTO jkyProductResDTO;

	            	// 判断是否第二次操作
	                if(isSku) {
	                	jkyProductResDTO = JkyProductUtil.selectJkyPage(pageNo, pageSize, goodsNo, null, null, startTime + " 00:00:00", endTime + " 00:00:00");
	                } else {
	                	jkyProductResDTO = JkyProductUtil.selectJkyPage(pageNo, pageSize, goodsNo, startTime + " 00:00:00", endTime + " 00:00:00", null, null);
	                }
	            	
	                if(null == jkyProductResDTO) {
	                    log.info("--------------- 吉客云获取商品接口请求失败 ---------------");
	                    continue;
	                }
	                List<Goods> goodsList = jkyProductResDTO.getResult().getDataResult().getGoods();
	                if(CollectionUtils.isEmpty(goodsList)) {
	                	log.info("--------------- 吉客云获取商品接口请求结束, 已到末尾 ---------------");
	                	break;
	                }
	                List<Goods> skuGoodsProcess = new ArrayList<>();
	                List<String> skuBarcodes = new ArrayList<>();
	                // 第二次查询需要去重(接口提供方无法优化)
            		repetGoods(goodsList, skuGoodsProcess, existGoodsNos, skuBarcodes, existSkuBarcodes);
            		if(CollectionUtils.isEmpty(skuGoodsProcess) || CollectionUtils.isEmpty(skuBarcodes)) {
	                    log.info("--------------- 吉客云获取商品接口无可用数据, 跳过该页: {} ---------------", pageNo);
	                    pageNo++;
            			continue;
            		}
	                // 商品保存
	                saveGoods(skuGoodsProcess);
	                /****** 根据商品查询sku数据 ******/
	                String strSkubarcode = StringUtils.join(skuBarcodes, ",");
	                // 获取对应仓库商品sku信息
	                long skuMillis = System.currentTimeMillis();
	                for(String warehouseCode : warehouseCodes) {
	                    int skuPageNo = 0;
	                    int skuPageSize = 200;
	                    while(true) {
	                        try {
	                            JkyProductSkuResDTO jkyProductSkuResDTO = jkyProductUtil.pageJkySkus(skuPageNo, skuPageSize, warehouseCode, strSkubarcode);
	                            if(null == jkyProductSkuResDTO) {
	                                log.info("--------------- 吉客云获取商品sku接口请求失败 ---------------");
	                                continue;
	                            }
	                            List<GoodsStockQuantity> skus = jkyProductSkuResDTO.getResult().getDataResult().getGoodsStockQuantity();
	                            if(CollectionUtils.isEmpty(skus)) {
	                            	log.info("--------------- 吉客云获取仓库: {} 商品, 日期: {}, sku完成 ---------------", warehouseCode, startTime);
	                            	break;
	                            }
	                            // sku保存
	                            saveGoodSkus(skus);
	                            log.info("--------------- 吉客云远程抓取{}仓库数据, 日期: {}, 第: {} 页, 有效数据: {}条 总耗时: {} 分  ---------------", warehouseCode, startTime, skuPageNo, skus.size(), (int)((System.currentTimeMillis() - skuMillis) / 1000 / 60));
	                        } catch (Exception e) {
	                            e.printStackTrace();
	                        }
	                        skuPageNo++;
	                    }
	                }
	                // 计算页数
	                Integer total = jkyProductResDTO.getResult().getPageInfo().getTotal();
	                pageCount = (total + pageSize - 1) / pageSize;
	            } catch (Exception e) {
	                e.printStackTrace();
	            }
	            log.info("--------------- 吉客云远程抓取主商品数据, 日期: {}, 第: {} 页,  总共: {} 页, 总耗时: {} 分  ---------------", startTime, pageNo, pageCount, (int)((System.currentTimeMillis() - millis) / 1000 / 60));
	            pageNo++;
	        }
            
            // 时间递增
//    		startTime = endTime;
//    		endTime = endTime.plusDays(1);
//		}
    }
    
    /**
     * 查重
     * @author daigb
     * @date:  2022-09-20 14:18
     */
    private void repetGoods(List<Goods> skuGoods, List<Goods> skuGoodsProcess, Map<String, String> existGoodsNos, List<String> skuBarcodes, Map<String, String> existSkuBarcodes) {
    	for(Goods goods : skuGoods) {
    		String goodsNo = goods.getGoodsNo();
    		String skuBarcode = goods.getSkuBarcode();
    		String isExistGoodsNo = existGoodsNos.get(goodsNo);
    		String isExistBarcode = existSkuBarcodes.get(skuBarcode);
    		
    		if(StringUtils.isNotBlank(goodsNo) && null == isExistGoodsNo) {
    			skuGoodsProcess.add(goods);
    			existGoodsNos.put(goodsNo, goodsNo);
    		}
    		if(StringUtils.isNotBlank(skuBarcode) && null == isExistBarcode) {
    			skuBarcodes.add(skuBarcode);
    			existSkuBarcodes.put(skuBarcode, skuBarcode);
    		}
    	}
    	
    }
    
    // 商品源数据保存
    private void saveGoods(List<Goods> goodsList) {
        // 商品数据
        List<JkyProductApi> goodsEntity = new ArrayList<>();
        for(Goods goods : goodsList) {
            JkyProductApi productEntity = new JkyProductApi();
            BeanUtils.copyProperties(goods, productEntity);
            productEntity.setImgUrlList(JSONArray.toJSONString(goods.getImgUrlList()));
            productEntity.setCreateTime(new Date());
            goodsEntity.add(productEntity);
        }
        jkyProductApiService.saveBatch(goodsEntity);
    }
    
    /**
     * 商品sku数据保存
     * @author daigb
     * @date:  2022-09-27 14:48
     */
    private void saveGoodSkus(List<GoodsStockQuantity> goodsSkuList) throws Exception {
        List<JkyProductSkuApi> productSkuEntity = new ArrayList<>();
        for(GoodsStockQuantity goodsSku : goodsSkuList) {
            JkyProductSkuApi skuEntity = new JkyProductSkuApi();
            BeanUtils.copyProperties(goodsSku, skuEntity);
            // 替换说明文字
            String skuName = skuEntity.getSkuName().replace("代购", "").replace("意现", "").replace("国现", "").replace("在途", "");
            if("001".equals(skuEntity.getWarehouseCode())) {
                skuEntity.setSkuName("国现" + skuName);
            } else if("004".equals(skuEntity.getWarehouseCode())) {
                skuEntity.setSkuName("意现" + skuName);
            } else if("KCTBXN".equals(skuEntity.getWarehouseCode())) {
                skuEntity.setSkuName("代购" + skuName);
            }
            // 查询对应价格(只有单商品查询, 只查询有库存的价格)
            //2023.3.7添加8F代购仓 库存为0 不执行 非8F代购仓 库存可以为0
            if( !"025".equals(goodsSku.getWarehouseCode()) || ("025".equals(goodsSku.getWarehouseCode()) && StringUtils.isNotBlank(goodsSku.getCurrentQuantity()) && !goodsSku.getCurrentQuantity().equals("0"))){
            	JkyProductSkuPriceResDTO jkyProductSkuPriceResDTO = jkyProductUtil.getJkySkuPrice(skuEntity.getSkuBarcode(), null);
            	List<Skuprice> skuprices = jkyProductSkuPriceResDTO.getResult().getDataResult().getSkupriceList();
            	for(Skuprice skuprice : skuprices) {
            		// 可用售价
            		if(0 == skuprice.getStatus()) {
            			skuEntity.setPrice(skuprice.getPrice1());
            			skuEntity.setOtPrice(skuprice.getPrice2());
            			skuEntity.setVipPrice(skuprice.getPrice3());
            			skuEntity.setOriginalPrice(skuprice.getPrice4());
            			skuEntity.setMinPrice(skuprice.getMinPrice());
            			skuEntity.setImgUrl(skuprice.getImgUrl());
            			break;
            		}
            	}
            }else
            {

                JkyProductApi jkyProductApi = jkyProductApiService.getOne(Wrappers.<JkyProductApi>lambdaQuery()
                    .eq(JkyProductApi::getGoodsNo, goodsSku.getGoodsNo())
                    .last("LIMIT 1")
                    );

                logsUtil.insertSyncProductLogs(goodsSku.getGoodsNo(),
                        goodsSku.getGoodsName(),goodsSku.getSkuName(),goodsSku.getSkuBarcode(),
                        goodsSku.getWarehouseCode() + ":库存是0","JKY",jkyProductApi.getCateName(),jkyProductApi.getBrandName(),0);

            }
        
            skuEntity.setCreateTime(new Date());
            productSkuEntity.add(skuEntity);
        }
        
        jkyProductSkuApiService.saveBatch(productSkuEntity);
    }

    @Override
    public Boolean pushProduct() {
        try {
            // 当次更新少于1千条, 则不更新, 并且重新拉取
            int count = jkyProductApiService.count();
//            if(1000 > count) {
//                systemConfigService.update(Wrappers.<SystemConfig>lambdaUpdate()
//                        .eq(SystemConfig :: getName, Constants.JKY_SYNC_STATUS)
//                        .set(SystemConfig :: getFormId, SyncStatusEnum.PUSH)
//                        .set(SystemConfig :: getUpdateTime, new Date())
//                        );
//                log.info("--------------- 吉客云商品源数据, 少于规定条数, 不进行下发, 总条数为: {} ---------------", count);
//                return false;
//            }
            
            // 将所有吉客云商品更新状态置为未更新
//            storeProductService.update(Wrappers.<StoreProduct>lambdaUpdate()
//                    .eq(StoreProduct :: getIsEurope, ProductTypeEnum.JKY.getCode())
//                    .set(StoreProduct :: getUpdateFlag, false)
//                    );
            
            // 商品更新操作
            int pageNo = 1;
            int pageSize = 100;
            int maxPage = (count + pageSize - 1) / pageSize;
            log.info("--------------- 开始分页查询吉客云商品源数据, 本次更新总条数为: {} ---------------", count);
            // 统计下放数据
            Map<String, Integer> supplierProductLogMap = new HashMap<>();
            supplierProductLogMap.put("nowPullCount", 0);
            supplierProductLogMap.put("nowNewPullCount", 0);
            
            long millis = System.currentTimeMillis();
            while (pageNo <= maxPage) {
                try {
                    PageHelper.startPage(pageNo, pageSize);
                    List<JkyProductApi> jkyProducts = jkyProductApiService.list(Wrappers.<JkyProductApi>lambdaQuery()
                    		);
                    PageInfo<JkyProductApi> pageInfo = PageInfo.of(jkyProducts);
                    List<JkyProductApi> products = pageInfo.getList();
                    buildProduct(products, supplierProductLogMap);
                } catch (Exception e) {
                    e.printStackTrace();
                }

                log.info("--------------- 吉客云下发商品到数据库, 当前页数: {}, 总页数: {}, 耗时: {} 分 ---------------", pageNo, maxPage, (int)((System.currentTimeMillis() - millis) / 1000 / 60));
                pageNo++;
            }

            // 下架所有未更新数据
//            storeProductService.update(Wrappers.<StoreProduct>lambdaUpdate()
//                    .eq(StoreProduct::getIsEurope, ProductTypeEnum.JKY.getCode())
//                    .eq(StoreProduct::getUpdateFlag, false)
//                    .eq(StoreProduct :: getIsShow, true)
//                    .eq(StoreProduct :: getIsDel, false)
//                    .set(StoreProduct::getIsShow, false)
//                    .set(StoreProduct::getStock, 0)
//                    .set(StoreProduct::getLastPushTime, new Date())
//                    .set(StoreProduct::getIsTusou, false)
//                    );
            // 下架所有sku库存
//            storeProductService.downloadSku(ProductTypeEnum.JKY.getCode());
            
            // 保存本次数据记录
            int productCount = storeProductService.count(Wrappers.<StoreProduct>lambdaQuery()
                    .eq(StoreProduct::getIsEurope, ProductTypeEnum.JKY.getCode())
                    .eq(StoreProduct :: getIsShow, true)
                    .eq(StoreProduct :: getIsDel, false)
            		);
            int productValueCount = storeProductAttrValueService.count(Wrappers.<StoreProductAttrValue>lambdaQuery()
            		.likeRight(StoreProductAttrValue :: getBarCode, BarCodeEnum.JKY.getCode())
            		);
            SupplierProductLog supplierProductLog = new SupplierProductLog();
            supplierProductLog.setSupplierName(ProductTypeEnum.JKY.getMsg());
            supplierProductLog.setNowNewPullCount(supplierProductLogMap.get("nowNewPullCount"));
            supplierProductLog.setNowPullCount(supplierProductLogMap.get("nowPullCount"));
            supplierProductLog.setProductCount(productCount);
            supplierProductLog.setProductValueCount(productValueCount);
            supplierProductLogService.save(supplierProductLog);

            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        return false;
    }
    
    /**
     * 推送自有商品到吉客云(改为推送指定专区商品到吉客云)
     */
//    @Override
//    public void remotePushProduct() {
//        try {
//            // 库存置空
//            jkyContrastService.update(Wrappers.<JkyContrast>lambdaUpdate()
//            		.set(JkyContrast :: getStock, 0)
//            		);
//            // 待出库商品
//            List<StockOutDetailView> stockOutDetailViews = new ArrayList<>();
//            // 待创建sku
//            List<BaseRequestBizData> skus = new ArrayList<>();
//            // 待创建价格
//            List<BaseRequestBizData> prices = new ArrayList<>();
//            // 待入库
//            List<StockInDetailView> stockIn = new ArrayList<>();
//            // 查询商户费率
//            ChannelMerchant channelMerchant = channelMerchantService.getOne(Wrappers.<ChannelMerchant>lambdaQuery()
//                    .eq(ChannelMerchant :: getChannelName, "吉客云")
//                    );
//            // 查询专区商品
//            List<StoreZoneProduct> zoneProducts = zoneProductService.list(Wrappers.<StoreZoneProduct>lambdaQuery()
//            		.in(StoreZoneProduct :: getZoneId, 759, 873) // 目前只推送这个专区商品
//            		.select(StoreZoneProduct :: getProductId)
//            		);
//            List<Integer> zoneProductIds = zoneProducts.stream().map(obj -> obj.getProductId()).collect(Collectors.toList());
//            // 临时使用该字段存储
//            JSONArray jsonArray = JSONArray.parseArray(channelMerchant.getBackUrl());
//            LambdaQueryWrapper<StoreProduct> lambdaQueryWrapper =  new LambdaQueryWrapper<>();
//            lambdaQueryWrapper.eq(StoreProduct :: getIsDel, false);
//            lambdaQueryWrapper.in(StoreProduct :: getId, zoneProductIds);
////            lambdaQueryWrapper.eq(StoreProduct :: getId, 184823620);
//            lambdaQueryWrapper.and(ilqw -> jsonArray.forEach(
//            		o -> {
//            			JSONObject jsonObject = JSONObject.parseObject(JSON.toJSONString(o));
//                        JSONArray suppliersId = jsonObject.getJSONArray("suppliersId");
//                    	JSONArray brandIds = jsonObject.getJSONArray("brandIds");
//                    	JSONArray cateIds = jsonObject.getJSONArray("cateIds");
//                    	JSONArray tempIds = jsonObject.getJSONArray("tempIds");
//                    	JSONArray seasonCodes = jsonObject.getJSONArray("seasonCodes");
//
//            			ilqw.or(iilqw -> iilqw.in(StoreProduct :: getSuppliersId, suppliersId)
//            					.in(0 < brandIds.size(), StoreProduct :: getBrandId, brandIds)
//            					.in(0 < cateIds.size(), StoreProduct :: getCateId, cateIds)
//            					.in(0 < tempIds.size(), StoreProduct :: getTempId, tempIds)
//            					.in(0 < seasonCodes.size(), StoreProduct :: getSeasonCode, seasonCodes)
//                    	);
//
//            		}
//            		));
//
//            MarkupRate markupRateEntity = markupRateService.getOne(Wrappers.<MarkupRate>lambdaQuery()
//                    .eq(MarkupRate::getMerId, channelMerchant.getId())
//                    .eq(MarkupRate ::getType, MarkupRateTypeEnum.PLAT_平台加价率.getType())
//                    .last("LIMIT 1"));
//            BigDecimal markupRate = markupRateEntity.getRatio();
//            int count = storeProductService.count(lambdaQueryWrapper);
//            // 商品推送
//            int pageNo = 1;
//            int pageSize = 200;
//            int maxPage = (count + pageSize - 1) / pageSize;
//            log.info("--------------- 开始分页查询吉客云商品源数据, 本次推送总条数为: {} ---------------", count);
//
//            long millis = System.currentTimeMillis();
//            while (pageNo <= maxPage) {
//                try {
//                    PageHelper.startPage(pageNo, pageSize);
//                    List<StoreProduct> storeProducts = storeProductService.list(lambdaQueryWrapper);
//                    PageInfo<StoreProduct> pageInfo = PageInfo.of(storeProducts);
//                    List<StoreProduct> products = pageInfo.getList();
//                    pushProduct(products, markupRate, stockOutDetailViews, skus, prices, stockIn);
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
//
//                log.info("--------------- 推送本地商品到吉客云, 当前页数: {}, 总页数: {}, 耗时: {} 分 ---------------", pageNo, maxPage, (int)((System.currentTimeMillis() - millis) / 1000 / 60));
//                pageNo++;
//            }
//
//            // 批次进行入库
//            if(pageNo > maxPage) {
//                // 批量出库(正常商品和已下架)
//            	List<StockOutDetailView> stockOutDetailViews2 = stockOutDetailViews.stream().distinct().collect(Collectors.toList());
//            	int outFlag = 0;
//        		while(outFlag <= stockOutDetailViews2.size()) {
//        			int index = outFlag + 150 > stockOutDetailViews2.size() ? stockOutDetailViews2.size() : outFlag + 150;
//        			List<StockOutDetailView> stockOutDetailViews3 = stockOutDetailViews2.subList(outFlag, index);
//        			outFlag += 150;
//                    JkyProductUtil.createJkyGoodsStockOut(stockOutDetailViews3);
//        			log.info("--------------- 推送本地商品到吉客云, 出库, 当前下标: {}, 总数量: {} ---------------", outFlag, stockOutDetailViews2.size());
//        		}
//                // 批量创建货品
//            	int createGoodsFlag = 0;
//        		while(createGoodsFlag <= skus.size()) {
//        			int index = createGoodsFlag + 150 > skus.size() ? skus.size() : createGoodsFlag + 150;
//        			List<BaseRequestBizData> skus2 = skus.subList(createGoodsFlag, index);
//        			createGoodsFlag += 150;
//                    String result = JackyunOpenHttpUtils.post("erp.goods.skuimportbatch", "v1.0", skus2);
//                    log.info("--------------- 吉客云创建货品: {}", result);
//                    if(500 == JSONObject.parseObject(result).getInteger("code")) {
//                    	log.info("--------------- 吉客云创建货品异常: {}", result);
//                    }
//        		}
//
//                // 批量创建sku价格信息
//            	int createSkuFlag = 0;
//        		while(createSkuFlag <= prices.size()) {
//        			int index = createSkuFlag + 150 > prices.size() ? prices.size() : createSkuFlag + 150;
//        			List<BaseRequestBizData> prices2 = prices.subList(createSkuFlag, index);
//        			createSkuFlag += 150;
//                    JkyProductUtil.createJkyGoodsSku(prices2);
//        			log.info("--------------- 推送本地商品到吉客云, 创建价格, 当前下标: {}, 总数量: {} ---------------", createSkuFlag, prices.size());
//        		}
//                // 批量入库
//        		int inFlag = 0;
//        		while(inFlag <= stockIn.size()) {
//        			int index = inFlag + 150 > stockIn.size() ? stockIn.size() : inFlag + 150;
//        			List<StockInDetailView> stockIn2 = stockIn.subList(inFlag, index);
//        			inFlag += 150;
//                    JkyProductUtil.createJkyGoodsStockIn(stockIn2);
//        			log.info("--------------- 推送本地商品到吉客云, 入库, 当前下标: {}, 总数量: {} ---------------", inFlag, stockIn.size());
//        		}
//            }
//
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }
    
    /**
     * 创建吉客云商品
     * @Description:  
     * @author daigb 
     * @param @param products
     * @param @param markupRate
     * @param @throws Exception
     * @return void
     * @throws
     */
    private void pushProduct(List<StoreProduct> products, BigDecimal markupRate, List<StockOutDetailView> stockOutDetailViews, 
    		List<BaseRequestBizData> skus, List<BaseRequestBizData> prices, List<StockInDetailView> stockIn) throws Exception {
        for(StoreProduct product : products) {
            // 下架商品
            if(product.getIsShow()) {
                // 组装商品信息
                CategoryMatch categoryMatch = categoryMatchService.getById(product.getCateId());
                Category category = new Category();
                BeanUtils.copyProperties(categoryMatch, category, "type");
                category.setType(categoryMatch.getType().getCode());
                List<StoreProductAttrValue> values = storeProductAttrValueService.list(Wrappers.<StoreProductAttrValue>lambdaQuery()
                        .eq(StoreProductAttrValue :: getProductId, product.getId())
                        );

                // 商品sku合并
                Map<String, StoreProductAttrValue> valueMergeMaps = new HashMap<>();
                for(StoreProductAttrValue value : values) {
                	StoreProductAttrValue valueMap = valueMergeMaps.get(value.getSuk());
                	if(null == valueMap) {
                		valueMergeMaps.put(value.getSuk(), value);
                	} else {
                		int stock = valueMap.getStock() + value.getStock();
                		BigDecimal price = value.getPrice().compareTo(valueMap.getPrice()) == -1 ? value.getPrice() : valueMap.getPrice();
                		valueMap.setStock(stock);
                		valueMap.setPrice(price);
                	}
                }
                List<StoreProductAttrValue> valueMerges = valueMergeMaps.values().stream().collect(Collectors.toList());
                
                Map<String, Object> map = jkyProductUtil.createJkySkus(product, valueMerges, category, markupRate);
                @SuppressWarnings("unchecked")
                List<BaseRequestBizData> goodsList = (List<BaseRequestBizData>) map.get("goodsList");
                @SuppressWarnings("unchecked")
                List<BaseRequestBizData> jsonStrs = (List<BaseRequestBizData>) map.get("jsonStrs");
                @SuppressWarnings("unchecked")
                List<StockInDetailView> stockInDetailViews = (List<StockInDetailView>) map.get("stockInDetailViews");

                // 待出库
                if(CollectionUtils.isNotEmpty(goodsList)) {
                	JackyunGoodsAddRequest g = (JackyunGoodsAddRequest)goodsList.get(0);
                	List<StockOutDetailView> views = jkyProductUtil.selectOutJkyGoods(g.getGoodsNo());
                	stockOutDetailViews.addAll(views);
                }
                
                skus.addAll(goodsList);
                prices.addAll(jsonStrs);
                stockIn.addAll(stockInDetailViews);
            }
        }
    }
    
    /**
     * 下架商品并置空库存
     * @author daigb
     * @date:  2022-05-11 15:52
     */
    private void shutdowmProduct(String barCode, String warehouseCode, String colorCode) {
		String colorCodeString = StringUtils.isBlank(colorCode) ? "" : "-" + colorCode;

        log.info("--------------- 下架商品并置空库存记录下日志测试, barCode: {}, warehouseCode: {} , " +
                "colorCode: {} ,colorCodeString: {}---------------", barCode,warehouseCode, colorCode,colorCodeString);

//        if(StringUtils.isNotBlank(warehouseCode)) {
//			barCode = barCode + "-" + warehouseCode + colorCodeString;
//			shutdowmProduct(barCode);
//		} else {
//			barCode = barCode + "-";
//			shutdowmProductLikeCode(barCode);
//		}
        
    }
    
    private void shutdowmProduct(String barCode) {
        // 图搜删除
//        storeProductService.delSeachImageByBarcode(barCode);
        // 商品下架
    	storeProductService.update(Wrappers.<StoreProduct>lambdaUpdate()
    			.eq(StoreProduct::getBarCode, barCode)
    			.eq(StoreProduct::getIsEurope, ProductTypeEnum.JKY.getCode())
    			.eq(StoreProduct :: getIsShow, true)
    			.eq(StoreProduct :: getIsDel, false)
    			.set(StoreProduct::getIsShow, false)
    			.set(StoreProduct::getStock, 0)
    			.set(StoreProduct::getLastPushTime, new Date())
    			.set(StoreProduct::getIsTusou, false)
    			);
    	storeProductAttrValueService.update(Wrappers.<StoreProductAttrValue>lambdaUpdate()
    			.eq(StoreProductAttrValue :: getBarCode, barCode)
    			.set(StoreProductAttrValue :: getStock, 0)
    			);
    }
    
    private void shutdowmProductLikeCode(String barCode) {
    	// 图搜删除
//        storeProductService.delSeachImageByBarcode(barCode);
    	// 商品下架
    	storeProductService.update(Wrappers.<StoreProduct>lambdaUpdate()
    			.likeRight(StoreProduct::getBarCode, barCode)
    			.eq(StoreProduct::getIsEurope, ProductTypeEnum.JKY.getCode())
    			.eq(StoreProduct :: getIsShow, true)
    			.eq(StoreProduct :: getIsDel, false)
    			.set(StoreProduct::getIsShow, false)
    			.set(StoreProduct::getStock, 0)
    			.set(StoreProduct::getLastPushTime, new Date())
    			.set(StoreProduct::getIsTusou, false)
    			);
    	storeProductAttrValueService.update(Wrappers.<StoreProductAttrValue>lambdaUpdate()
    			.likeRight(StoreProductAttrValue :: getBarCode, barCode)
    			.set(StoreProductAttrValue :: getStock, 0)
    			);
    }
    
    /**
     * 商品构建处理
     * @Description:  
     * @author daigb 
     * @param @param products
     * @return void
     * @throws
     */
    private void buildProduct(List<JkyProductApi> products, Map<String, Integer> supplierProductLogMap) {
        try {
            for (JkyProductApi product : products) {
                String barCode = BarCodeEnum.JKY.getCode() + product.getGoodsNo().toString();
            	// 是否是新商品
            	boolean isNewProduct = false;
            	// 是否成功下放
            	boolean isPullProduct = false;
                List<JkyProductSkuApi> skus = jkyProductSkuApiService.list(Wrappers.<JkyProductSkuApi>lambdaQuery()
                        .eq(JkyProductSkuApi :: getGoodsId, product.getGoodsId())
//                        .isNotNull(JkyProductSkuApi :: getPrice)
                        .isNotNull(JkyProductSkuApi::getSkuName)
                        // 过滤可售仓库
                        //.eq(JkyProductSkuApi :: getWarehouseCode, "001")
//                        .gt(JkyProductSkuApi :: getCurrentQuantity, 0)
                        );
                if (CollectionUtils.isEmpty(skus)) {
                	shutdowmProduct(barCode, null, product.getColorCode());
                    continue;
                }
                // 童装逻辑
                Boolean isBonpoint = false;
                if("46".equals(product.getBrandId())) {
                	isBonpoint = true;
                	if(!product.getGoodsNo().contains(" ")) {
                    	shutdowmProduct(barCode, null, product.getColorCode());
                		continue;
                	}
                }
                // 库存计算
                boolean is001 = false;
                boolean is066 = false;
                boolean is075 = false;
                boolean is086 = false;
                boolean is004 = false;
                boolean isKCTBXN = false;
                boolean is025 = false;
                boolean is018 = false;
                boolean isTfx = false;
                boolean isZTXHC = false;
                Map<String, List<JkyProductSkuApi>> skus001 = new HashMap<>();
                Map<String, List<JkyProductApi>> products001 = new HashMap<>();
                Map<String, List<JkyProductSkuApi>> skus066 = new HashMap<>();
                Map<String, List<JkyProductApi>> products066 = new HashMap<>();
                Map<String, List<JkyProductSkuApi>> skus075 = new HashMap<>();
                Map<String, List<JkyProductApi>> products075 = new HashMap<>();
                Map<String, List<JkyProductSkuApi>> skus086 = new HashMap<>();
                Map<String, List<JkyProductApi>> products086 = new HashMap<>();
                Map<String, List<JkyProductSkuApi>>  skus004 = new HashMap<>();
                Map<String, List<JkyProductApi>> products004 = new HashMap<>();
                Map<String, List<JkyProductSkuApi>>  skusKCTBXN = new HashMap<>();
                Map<String, List<JkyProductApi>> productsKCTBXN = new HashMap<>();
                Map<String, List<JkyProductSkuApi>>  skus025 = new HashMap<>();
                Map<String, List<JkyProductApi>> products025 = new HashMap<>();
                Map<String, List<JkyProductSkuApi>>  skus018 = new HashMap<>();
                Map<String, List<JkyProductApi>> products018 = new HashMap<>();
                Map<String, List<JkyProductSkuApi>>  skusZTXHC = new HashMap<>();
                Map<String, List<JkyProductApi>> productsZTXHC = new HashMap<>();
                for (JkyProductSkuApi sku : skus) {
                	String skuName = sku.getSkuName().replaceAll("（", "").replaceAll("）", "").replaceAll("代购", "")
                			.replaceAll("意现", "").replaceAll("国现", "").replaceAll("在途", "").replaceAll("黑色", "")
                			.replaceAll("白色", "").replaceAll("黄色", "").replaceAll("红色", "").replaceAll("蓝色", "")
                			.replaceAll("绿色", "").replaceAll("米色", "").replaceAll("藏蓝色", "").replaceAll("深蓝色", "")
                			.replaceAll("灰色", "").replaceAll("粉色", "").replaceAll("驼色", "").replaceAll("中灰色", "")
                			.replaceAll("粉红", "").replaceAll("酒红色", "").replaceAll("深蓝", "")
                			;
                    String colorName = isBonpoint ? product.getGoodsNo().split(" ")[1] : skuName.split(";")[0];

//            		int lockedQuantity = null == sku.getLockedQuantity() ? 0 : Integer.parseInt(sku.getLockedQuantity());
                    if("001".equals(sku.getWarehouseCode())) {
                		// 色号拆分sku
                		List<JkyProductSkuApi> jkySkus = null == skus001.get(colorName) ? new ArrayList<>() : skus001.get(colorName);
                		jkySkus.add(sku);
                		skus001.put(colorName, jkySkus);
                		
                		// 色号拆分商品
                		List<JkyProductApi> jkyProducts = null == products001.get(colorName) ? new ArrayList<>() : products001.get(colorName);
                		JkyProductApi jkyProductApi = new JkyProductApi();
                		BeanUtils.copyProperties(product, jkyProductApi);
                		jkyProductApi.setColorCode(colorName);
                		jkyProducts.add(jkyProductApi);
                		products001.put(colorName, jkyProducts);
                		
                		is001 = true;
                	} else if("066".equals(sku.getWarehouseCode())) {
                		// 色号拆分sku
                		List<JkyProductSkuApi> jkySkus = null == skus066.get(colorName) ? new ArrayList<>() : skus066.get(colorName);
                		jkySkus.add(sku);
                		skus066.put(colorName, jkySkus);
                		
                		// 色号拆分商品
                		List<JkyProductApi> jkyProducts = null == products066.get(colorName) ? new ArrayList<>() : products066.get(colorName);
                		JkyProductApi jkyProductApi = new JkyProductApi();
                		BeanUtils.copyProperties(product, jkyProductApi);
                		jkyProductApi.setColorCode(colorName);
                		jkyProducts.add(jkyProductApi);
                		products066.put(colorName, jkyProducts);
                		
                		is066 = true;
                	} else if("075".equals(sku.getWarehouseCode())) {
                		// 色号拆分sku
                		List<JkyProductSkuApi> jkySkus = null == skus075.get(colorName) ? new ArrayList<>() : skus075.get(colorName);
                		jkySkus.add(sku);
                		skus075.put(colorName, jkySkus);
                		
                		// 色号拆分商品
                		List<JkyProductApi> jkyProducts = null == products075.get(colorName) ? new ArrayList<>() : products075.get(colorName);
                		JkyProductApi jkyProductApi = new JkyProductApi();
                		BeanUtils.copyProperties(product, jkyProductApi);
                		jkyProductApi.setColorCode(colorName);
                		jkyProducts.add(jkyProductApi);
                		products075.put(colorName, jkyProducts);
                		
                		is075 = true;
                	} else if("086".equals(sku.getWarehouseCode())) {
                		// 色号拆分sku
                		List<JkyProductSkuApi> jkySkus = null == skus086.get(colorName) ? new ArrayList<>() : skus086.get(colorName);
                		jkySkus.add(sku);
                		skus086.put(colorName, jkySkus);
                		
                		// 色号拆分商品
                		List<JkyProductApi> jkyProducts = null == products086.get(colorName) ? new ArrayList<>() : products086.get(colorName);
                		JkyProductApi jkyProductApi = new JkyProductApi();
                		BeanUtils.copyProperties(product, jkyProductApi);
                		jkyProductApi.setColorCode(colorName);
                		jkyProducts.add(jkyProductApi);
                		products086.put(colorName, jkyProducts);
                		
                		is086 = true;
                	} else if("004".equals(sku.getWarehouseCode())) {
                		// 色号拆分sku
                		List<JkyProductSkuApi> jkySkus = null == skus004.get(colorName) ? new ArrayList<>() : skus004.get(colorName);
                		jkySkus.add(sku);
                		skus004.put(colorName, jkySkus);
                		
                		// 色号拆分商品
                		List<JkyProductApi> jkyProducts = null == products004.get(colorName) ? new ArrayList<>() : products004.get(colorName);
                		JkyProductApi jkyProductApi = new JkyProductApi();
                		BeanUtils.copyProperties(product, jkyProductApi);
                		jkyProductApi.setColorCode(colorName);
                		jkyProducts.add(jkyProductApi);
                		products004.put(colorName, jkyProducts);

                		is004 = true;
                	} else if("KCTBXN".equals(sku.getWarehouseCode())) {
                		// 色号拆分sku
                		List<JkyProductSkuApi> jkySkus = null == skusKCTBXN.get(colorName) ? new ArrayList<>() : skusKCTBXN.get(colorName);
                		jkySkus.add(sku);
                		skusKCTBXN.put(colorName, jkySkus);
                		
                		// 色号拆分商品
                		List<JkyProductApi> jkyProducts = null == productsKCTBXN.get(colorName) ? new ArrayList<>() : productsKCTBXN.get(colorName);
                		JkyProductApi jkyProductApi = new JkyProductApi();
                		BeanUtils.copyProperties(product, jkyProductApi);
                		jkyProductApi.setColorCode(colorName);
                		jkyProducts.add(jkyProductApi);
                		productsKCTBXN.put(colorName, jkyProducts);

                		isKCTBXN = true;
                	} else if("ZTXHC".equals(sku.getWarehouseCode())) {
                        // 色号拆分sku
                        List<JkyProductSkuApi> jkySkus = null == skusZTXHC.get(colorName) ? new ArrayList<>() : skusZTXHC.get(colorName);
                        jkySkus.add(sku);
                        skusZTXHC.put(colorName, jkySkus);

                        // 色号拆分商品
                        List<JkyProductApi> jkyProducts = null == productsZTXHC.get(colorName) ? new ArrayList<>() : productsZTXHC.get(colorName);
                        JkyProductApi jkyProductApi = new JkyProductApi();
                        BeanUtils.copyProperties(product, jkyProductApi);
                        jkyProductApi.setColorCode(colorName);
                        jkyProducts.add(jkyProductApi);
                        productsZTXHC.put(colorName, jkyProducts);

                        isZTXHC = true;
                    }else if("025".equals(sku.getWarehouseCode())) {
                		// 色号拆分sku
                		List<JkyProductSkuApi> jkySkus = null == skus025.get(colorName) ? new ArrayList<>() : skus025.get(colorName);
                		jkySkus.add(sku);
                		skus025.put(colorName, jkySkus);
                		
                		// 色号拆分商品
                		List<JkyProductApi> jkyProducts = null == products025.get(colorName) ? new ArrayList<>() : products025.get(colorName);
                		JkyProductApi jkyProductApi = new JkyProductApi();
                		BeanUtils.copyProperties(product, jkyProductApi);
                		jkyProductApi.setColorCode(colorName);
                		jkyProducts.add(jkyProductApi);
                		products025.put(colorName, jkyProducts);

                		is025 = true;
                	} else if("018".equals(sku.getWarehouseCode())) {
                		// 色号拆分sku
                		List<JkyProductSkuApi> jkySkus = null == skus018.get(colorName) ? new ArrayList<>() : skus018.get(colorName);
                		jkySkus.add(sku);
                		skus018.put(colorName, jkySkus);
                		
                		// 色号拆分商品
                		List<JkyProductApi> jkyProducts = null == products018.get(colorName) ? new ArrayList<>() : products018.get(colorName);
                		JkyProductApi jkyProductApi = new JkyProductApi();
                		BeanUtils.copyProperties(product, jkyProductApi);
                		jkyProductApi.setColorCode(colorName);
                		jkyProducts.add(jkyProductApi);
                		products018.put(colorName, jkyProducts);

                		is018 = true;
                	} else if("tfx".equals(sku.getWarehouseCode())) {
                		// 色号拆分sku
//                		List<JkyProductSkuApi> jkySkus = null == skusTfx.get(colorName) ? new ArrayList<>() : skusTfx.get(colorName);
//                		jkySkus.add(sku);
//                		skusTfx.put(colorName, jkySkus);
                		
                		// 色号拆分商品
//                		List<JkyProductApi> jkyProducts = null == productsTfx.get(colorName) ? new ArrayList<>() : productsTfx.get(colorName);
//                		JkyProductApi jkyProductApi = new JkyProductApi();
//                		BeanUtils.copyProperties(product, jkyProductApi);
//                		jkyProducts.add(jkyProductApi);
//                		productsTfx.put(colorName, jkyProducts);

                		isTfx = true;
                	}
                }
                if(is001) {
                	// 多颜色单仓库
                	for(String key : products001.keySet()) {
	            		JkyProductApi jkyProductApi = products001.get(key).get(0);
	            		List<JkyProductSkuApi> jkySkus = skus001.get(key);
            			// 构建实体
            			StoreProductCrmebRequest storeProductCrmebRequest = sourceJkyProductUtil.buildJkyProduct(jkyProductApi, jkySkus, "001");
            			if(null == storeProductCrmebRequest) {
                        	shutdowmProduct(barCode, "001", key);
            				continue;
            			}
        				// 数据保存
        				StoreProduct storeProduct = storeProductService.getByBarCode(storeProductCrmebRequest.getBarCode(), storeProductCrmebRequest.getSuppliersId(), storeProductCrmebRequest.getKeyword());
        				boolean crmebUpdate = true;
						if (null != storeProduct && null != storeProduct.getLastPushTime()) {
							/*long time = (System.currentTimeMillis() - storeProduct.getLastPushTime().getTime()) / 1000 / 60;
							if (60 > time) {
								log.info("--------------- 该商品: {} 上次吉客云更新时间: {} 小于一小时, 不进行更新 ---------------", storeProductCrmebRequest.getBarCode(), storeProduct.getLastPushTime());
								crmebUpdate = false;
								storeProductService.update(Wrappers.<StoreProduct>lambdaUpdate()
										.eq(StoreProduct::getId, storeProduct.getId())
										.set(StoreProduct::getUpdateFlag, true)
										);
							}*/
						} else if(null == storeProduct) {
							isNewProduct = true;
						}
        				if (crmebUpdate) {
        					storeProductService.saveOtProduct(storeProductCrmebRequest, storeProduct, storeProductCrmebRequest.getStock());
        				}
        				isPullProduct = true;
                	}
                }
                if(is066) {
                	// 多颜色单仓库
                	for(String key : products066.keySet()) {
                		JkyProductApi jkyProductApi = products066.get(key).get(0);
                		List<JkyProductSkuApi> jkySkus = skus066.get(key);
                		// 构建实体
                		StoreProductCrmebRequest storeProductCrmebRequest = sourceJkyProductUtil.buildJkyProduct(jkyProductApi, jkySkus, "066");
                		if(null == storeProductCrmebRequest) {
                			shutdowmProduct(barCode, "066", key);
                			continue;
                		}
                		// 数据保存
                		StoreProduct storeProduct = storeProductService.getByBarCode(storeProductCrmebRequest.getBarCode(), storeProductCrmebRequest.getSuppliersId(), storeProductCrmebRequest.getKeyword());
                		boolean crmebUpdate = true;
                		if (null != storeProduct && null != storeProduct.getLastPushTime()) {
                			/*long time = (System.currentTimeMillis() - storeProduct.getLastPushTime().getTime()) / 1000 / 60;
							if (60 > time) {
								log.info("--------------- 该商品: {} 上次吉客云更新时间: {} 小于一小时, 不进行更新 ---------------", storeProductCrmebRequest.getBarCode(), storeProduct.getLastPushTime());
								crmebUpdate = false;
								storeProductService.update(Wrappers.<StoreProduct>lambdaUpdate()
										.eq(StoreProduct::getId, storeProduct.getId())
										.set(StoreProduct::getUpdateFlag, true)
										);
							}*/
                		} else if(null == storeProduct) {
                			isNewProduct = true;
                		}
                		if (crmebUpdate) {
                			storeProductService.saveOtProduct(storeProductCrmebRequest, storeProduct, storeProductCrmebRequest.getStock());
                		}
                		isPullProduct = true;
                	}
                }
                if(is075) {
                	// 多颜色单仓库
                	for(String key : products075.keySet()) {
                		JkyProductApi jkyProductApi = products075.get(key).get(0);
                		List<JkyProductSkuApi> jkySkus = skus075.get(key);
                		// 构建实体
                		StoreProductCrmebRequest storeProductCrmebRequest = sourceJkyProductUtil.buildJkyProduct(jkyProductApi, jkySkus, "075");
                		if(null == storeProductCrmebRequest) {
                			shutdowmProduct(barCode, "075", key);
                			continue;
                		}
                		// 数据保存
                		StoreProduct storeProduct = storeProductService.getByBarCode(storeProductCrmebRequest.getBarCode(), storeProductCrmebRequest.getSuppliersId(), storeProductCrmebRequest.getKeyword());
                		boolean crmebUpdate = true;
                		if (null != storeProduct && null != storeProduct.getLastPushTime()) {
                			/*long time = (System.currentTimeMillis() - storeProduct.getLastPushTime().getTime()) / 1000 / 60;
							if (60 > time) {
								log.info("--------------- 该商品: {} 上次吉客云更新时间: {} 小于一小时, 不进行更新 ---------------", storeProductCrmebRequest.getBarCode(), storeProduct.getLastPushTime());
								crmebUpdate = false;
								storeProductService.update(Wrappers.<StoreProduct>lambdaUpdate()
										.eq(StoreProduct::getId, storeProduct.getId())
										.set(StoreProduct::getUpdateFlag, true)
										);
							}*/
                		} else if(null == storeProduct) {
                			isNewProduct = true;
                		}
                		if (crmebUpdate) {
                			storeProductService.saveOtProduct(storeProductCrmebRequest, storeProduct, storeProductCrmebRequest.getStock());
                		}
                		isPullProduct = true;
                	}
                }
                if(is086) {
                	// 多颜色单仓库
                	for(String key : products086.keySet()) {
                		JkyProductApi jkyProductApi = products086.get(key).get(0);
                		List<JkyProductSkuApi> jkySkus = skus086.get(key);
                		// 构建实体
                		StoreProductCrmebRequest storeProductCrmebRequest = sourceJkyProductUtil.buildJkyProduct(jkyProductApi, jkySkus, "086");
                		if(null == storeProductCrmebRequest) {
                			shutdowmProduct(barCode, "086", key);
                			continue;
                		}
                		// 数据保存
                		StoreProduct storeProduct = storeProductService.getByBarCode(storeProductCrmebRequest.getBarCode(), storeProductCrmebRequest.getSuppliersId(), storeProductCrmebRequest.getKeyword());
                		boolean crmebUpdate = true;
                		if (null != storeProduct && null != storeProduct.getLastPushTime()) {
                			/*long time = (System.currentTimeMillis() - storeProduct.getLastPushTime().getTime()) / 1000 / 60;
							if (60 > time) {
								log.info("--------------- 该商品: {} 上次吉客云更新时间: {} 小于一小时, 不进行更新 ---------------", storeProductCrmebRequest.getBarCode(), storeProduct.getLastPushTime());
								crmebUpdate = false;
								storeProductService.update(Wrappers.<StoreProduct>lambdaUpdate()
										.eq(StoreProduct::getId, storeProduct.getId())
										.set(StoreProduct::getUpdateFlag, true)
										);
							}*/
                		} else if(null == storeProduct) {
                			isNewProduct = true;
                		}
                		if (crmebUpdate) {
                			storeProductService.saveOtProduct(storeProductCrmebRequest, storeProduct, storeProductCrmebRequest.getStock());
                		}
                		isPullProduct = true;
                	}
                }
                if(is004) {
                	// 多颜色单仓库
                	for(String key : products004.keySet()) {
	            		JkyProductApi jkyProductApi = products004.get(key).get(0);
	            		List<JkyProductSkuApi> jkySkus = skus004.get(key);
            			// 构建实体
            			StoreProductCrmebRequest storeProductCrmebRequest = sourceJkyProductUtil.buildJkyProduct(jkyProductApi, jkySkus, "004");
            			if(null == storeProductCrmebRequest) {
                        	shutdowmProduct(barCode, "004", key);
            				continue;
            			}
        				// 数据保存
        				StoreProduct storeProduct = storeProductService.getByBarCode(storeProductCrmebRequest.getBarCode(), storeProductCrmebRequest.getSuppliersId(), storeProductCrmebRequest.getKeyword());
        				boolean crmebUpdate = true;
        				if (null != storeProduct && null != storeProduct.getLastPushTime()) {
							/*long time = (System.currentTimeMillis() - storeProduct.getLastPushTime().getTime()) / 1000 / 60;
							if (60 > time) {
								log.info("--------------- 该商品: {} 上次吉客云更新时间: {} 小于一小时, 不进行更新 ---------------", storeProductCrmebRequest.getBarCode(), storeProduct.getLastPushTime());
								crmebUpdate = false;
								storeProductService.update(Wrappers.<StoreProduct>lambdaUpdate()
										.eq(StoreProduct::getId, storeProduct.getId())
										.set(StoreProduct::getUpdateFlag, true)
										);
							}*/
        				} else if(null == storeProduct) {
        					isNewProduct = true;
        				}
        				if (crmebUpdate) {
        					storeProductService.saveOtProduct(storeProductCrmebRequest, storeProduct, storeProductCrmebRequest.getStock());
        				}
        				isPullProduct = true;
            		}
                }
                if(isKCTBXN) {
                	// 多颜色单仓库
                	for(String key : productsKCTBXN.keySet()) {
	            		JkyProductApi jkyProductApi = productsKCTBXN.get(key).get(0);
	            		List<JkyProductSkuApi> jkySkus = skusKCTBXN.get(key);
            			// 构建实体
            			StoreProductCrmebRequest storeProductCrmebRequest = sourceJkyProductUtil.buildJkyProduct(jkyProductApi, jkySkus, "KCTBXN");
            			if(null == storeProductCrmebRequest) {
                        	shutdowmProduct(barCode, "KCTBXN", key);
            				continue;
            			}
        				// 数据保存
        				StoreProduct storeProduct = storeProductService.getByBarCode(storeProductCrmebRequest.getBarCode(), storeProductCrmebRequest.getSuppliersId(), storeProductCrmebRequest.getKeyword());
        				boolean crmebUpdate = true;
        				if (null != storeProduct && null != storeProduct.getLastPushTime()) {
							/*long time = (System.currentTimeMillis() - storeProduct.getLastPushTime().getTime()) / 1000 / 60;
							if (60 > time) {
								log.info("--------------- 该商品: {} 上次吉客云更新时间: {} 小于一小时, 不进行更新 ---------------", storeProductCrmebRequest.getBarCode(), storeProduct.getLastPushTime());
								crmebUpdate = false;
								storeProductService.update(Wrappers.<StoreProduct>lambdaUpdate()
										.eq(StoreProduct::getId, storeProduct.getId())
										.set(StoreProduct::getUpdateFlag, true)
										);
							}*/
        				} else if(null == storeProduct) {
        					isNewProduct = true;
        				}
        				if (crmebUpdate) {
        					storeProductService.saveOtProduct(storeProductCrmebRequest, storeProduct, storeProductCrmebRequest.getStock());
        				}
        				isPullProduct = true;
                	}
                }
                if(isZTXHC) {
                    // 多颜色单仓库
                    for(String key : productsZTXHC.keySet()) {
                        JkyProductApi jkyProductApi = productsZTXHC.get(key).get(0);
                        List<JkyProductSkuApi> jkySkus = skusZTXHC.get(key);
                        // 构建实体
                        StoreProductCrmebRequest storeProductCrmebRequest = sourceJkyProductUtil.buildJkyProduct(jkyProductApi, jkySkus, "ZTXHC");
                        if(null == storeProductCrmebRequest) {
                            shutdowmProduct(barCode, "ZTXHC", key);
                            continue;
                        }
                        // 数据保存
                        StoreProduct storeProduct = storeProductService.getByBarCode(storeProductCrmebRequest.getBarCode(), storeProductCrmebRequest.getSuppliersId(), storeProductCrmebRequest.getKeyword());
                        boolean crmebUpdate = true;
                        if (null != storeProduct && null != storeProduct.getLastPushTime()) {
							/*long time = (System.currentTimeMillis() - storeProduct.getLastPushTime().getTime()) / 1000 / 60;
							if (60 > time) {
								log.info("--------------- 该商品: {} 上次吉客云更新时间: {} 小于一小时, 不进行更新 ---------------", storeProductCrmebRequest.getBarCode(), storeProduct.getLastPushTime());
								crmebUpdate = false;
								storeProductService.update(Wrappers.<StoreProduct>lambdaUpdate()
										.eq(StoreProduct::getId, storeProduct.getId())
										.set(StoreProduct::getUpdateFlag, true)
										);
							}*/
                        } else if(null == storeProduct) {
                            isNewProduct = true;
                        }
                        if (crmebUpdate) {
                            storeProductService.saveOtProduct(storeProductCrmebRequest, storeProduct, storeProductCrmebRequest.getStock());
                        }
                        isPullProduct = true;
                    }
                }
                if(is025) {
                	// 多颜色单仓库
                	for(String key : products025.keySet()) {
	            		JkyProductApi jkyProductApi = products025.get(key).get(0);
	            		List<JkyProductSkuApi> jkySkus = skus025.get(key);
            			// 构建实体
            			StoreProductCrmebRequest storeProductCrmebRequest = sourceJkyProductUtil.buildJkyProduct(jkyProductApi, jkySkus, "025");
            			if(null == storeProductCrmebRequest) {
                        	shutdowmProduct(barCode, "025", key);
            				continue;
            			}
        				// 数据保存
        				StoreProduct storeProduct = storeProductService.getByBarCode(storeProductCrmebRequest.getBarCode(), storeProductCrmebRequest.getSuppliersId(), storeProductCrmebRequest.getKeyword());
        				boolean crmebUpdate = true;
        				if (null != storeProduct && null != storeProduct.getLastPushTime()) {
							/*long time = (System.currentTimeMillis() - storeProduct.getLastPushTime().getTime()) / 1000 / 60;
							if (60 > time) {
								log.info("--------------- 该商品: {} 上次吉客云更新时间: {} 小于一小时, 不进行更新 ---------------", storeProductCrmebRequest.getBarCode(), storeProduct.getLastPushTime());
								crmebUpdate = false;
								storeProductService.update(Wrappers.<StoreProduct>lambdaUpdate()
										.eq(StoreProduct::getId, storeProduct.getId())
										.set(StoreProduct::getUpdateFlag, true)
										);
							}*/
        				} else if(null == storeProduct) {
        					isNewProduct = true;
        				}
        				if (crmebUpdate) {
        					storeProductService.saveOtProduct(storeProductCrmebRequest, storeProduct, storeProductCrmebRequest.getStock());
        				}
        				isPullProduct = true;
                	}
                }
                if(is018) {
                	// 多颜色单仓库
                	for(String key : products018.keySet()) {
	            		JkyProductApi jkyProductApi = products018.get(key).get(0);
	            		List<JkyProductSkuApi> jkySkus = skus018.get(key);
            			// 构建实体
            			StoreProductCrmebRequest storeProductCrmebRequest = sourceJkyProductUtil.buildJkyProduct(jkyProductApi, jkySkus, "018");
            			if(null == storeProductCrmebRequest) {
                        	shutdowmProduct(barCode, "018", key);
            				continue;
            			}
        				// 数据保存
        				StoreProduct storeProduct = storeProductService.getByBarCode(storeProductCrmebRequest.getBarCode(), storeProductCrmebRequest.getSuppliersId(), storeProductCrmebRequest.getKeyword());
        				boolean crmebUpdate = true;
        				if (null != storeProduct && null != storeProduct.getLastPushTime()) {
							/*long time = (System.currentTimeMillis() - storeProduct.getLastPushTime().getTime()) / 1000 / 60;
							if (60 > time) {
								log.info("--------------- 该商品: {} 上次吉客云更新时间: {} 小于一小时, 不进行更新 ---------------", storeProductCrmebRequest.getBarCode(), storeProduct.getLastPushTime());
								crmebUpdate = false;
								storeProductService.update(Wrappers.<StoreProduct>lambdaUpdate()
										.eq(StoreProduct::getId, storeProduct.getId())
										.set(StoreProduct::getUpdateFlag, true)
										);
							}*/
        				} else if(null == storeProduct) {
        					isNewProduct = true;
        				}
        				if (crmebUpdate) {
        					storeProductService.saveOtProduct(storeProductCrmebRequest, storeProduct, storeProductCrmebRequest.getStock());
        				}
        				isPullProduct = true;
                	}
                }
                // 淘分销仓与其他仓货品属性不同(不可复制)
                if(isTfx) {
            		// 构建实体
            		StoreProductCrmebRequest storeProductCrmebRequest = sourceJkyProductUtil.buildJkyProduct(product, skus, "tfx");
            		if(null == storeProductCrmebRequest) {
            			shutdowmProduct(barCode, "tfx", null);
            			continue;
            		}
            		// 数据保存
            		StoreProduct storeProduct = storeProductService.getByBarCode(storeProductCrmebRequest.getBarCode(), storeProductCrmebRequest.getSuppliersId(), storeProductCrmebRequest.getKeyword());
            		boolean crmebUpdate = true;
            		if (null != storeProduct && null != storeProduct.getLastPushTime()) {
            			/*long time = (System.currentTimeMillis() - storeProduct.getLastPushTime().getTime()) / 1000 / 60;
						if (60 > time) {
							log.info("--------------- 该商品: {} 上次吉客云更新时间: {} 小于一小时, 不进行更新 ---------------", storeProductCrmebRequest.getBarCode(), storeProduct.getLastPushTime());
							crmebUpdate = false;
							storeProductService.update(Wrappers.<StoreProduct>lambdaUpdate()
									.eq(StoreProduct::getId, storeProduct.getId())
									.set(StoreProduct::getUpdateFlag, true)
									);
						}*/
            		} else if(null == storeProduct) {
            			isNewProduct = true;
            		}
            		if (crmebUpdate) {
            			storeProductService.saveOtProduct(storeProductCrmebRequest, storeProduct, storeProductCrmebRequest.getStock());
            		}
            		isPullProduct = true;
                }
                
                if(isPullProduct) {
                	int nowPullCount = supplierProductLogMap.get("nowPullCount") + 1;
                	int nowNewPullCount = isNewProduct ? supplierProductLogMap.get("nowNewPullCount") + 1 : supplierProductLogMap.get("nowNewPullCount");
                	
                	supplierProductLogMap.put("nowPullCount", nowPullCount);
                	supplierProductLogMap.put("nowNewPullCount", nowNewPullCount);

                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
}
